package com.itheima.day20.demo06;

import java.io.*;
import java.util.Scanner;

public class demo09 {
    public static void main(String[] args) throws Exception {
        File src = getDir();
        File dest = getDir();
        copyDirectory(src, dest);
    }

    private static void copyDirectory(File src, File dest) throws Exception {
        File newFile = new File(dest, src.getName());
        if (!newFile.exists()) {
            newFile.mkdir();
        }
        File[] listFiles = src.listFiles();
        for (File file : listFiles) {
            if (file.isDirectory()) {
                copyDirectory(file, newFile);
            } else {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newFile, file.getName())));
                byte[] bys = new byte[1024];
                int len = 0;
                while ((len = bis.read(bys)) != -1) {
                    bos.write(bys, 0, len);
                }
                bos.close();
                bis.close();
            }
        }
    }

    private static File getDir() {
        System.out.println("请输入一个文件夹路径:");
        while (true) {
            Scanner sc = new Scanner(System.in);
            File file = new File(sc.nextLine());
            if (file.isDirectory())
                return file;
            else
                System.out.println("你输入的路径不合法，请重新输入！");
        }

    }
}
